package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * XiaoPanYu
 * on 2018/11/20
 */
public class DeliveryOrderAdapter extends BaseAdapter{

    private List<StockPOListItem> listItems;
    private Drawable yuan;
    private Drawable xuanzhong;

    public int getNumber(){
        return number;
    }

    public void setNumber(int number){
        this.number = number;
    }

    private int number=0;

    public DeliveryOrderAdapter(List<StockPOListItem> listItems){
        this.listItems = listItems;
    }


    public void setYuan(Drawable drawable){
        this.yuan = drawable;
    }

    public void setXuanzhong(Drawable drawable){
        this.xuanzhong = drawable;
    }

    @Override
    public int getCount(){
        return listItems.size();
    }

    @Override
    public Object getItem(int i){
        return listItems.get(i);
    }

    @Override
    public long getItemId(int i){
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup){
        if(view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_delivery_order_item, null);
            ViewHolder holder =new ViewHolder(view);
            view.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        StockPOListItem item = listItems.get(i);
        holder.sn.setText(item.getDevNo());
        holder.price.setText(item.getPrice());
        if(item.isChoice()){
            holder.choice.setImageDrawable(xuanzhong);
        }else {
            holder.choice.setImageDrawable(yuan);
        }
        holder.choice.setOnClickListener(new OnClickListener(i));

        return view;
    }

    static class ViewHolder{
        @BindView(R.id.sn)
        TextView sn;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.choice)
        ImageView choice;

        ViewHolder(View view){
            ButterKnife.bind(this, view);
        }
    }

    class OnClickListener implements View.OnClickListener{
        private int i;

        OnClickListener(int i){
            this.i = i;
        }

        @Override
        public void onClick(View view){
            StockPOListItem item = listItems.get(i);
            ImageView icon = (ImageView) view;
            if(item.isChoice()){
                DeliveryOrderAdapter.this.setNumber(DeliveryOrderAdapter.this.getNumber()-1);
                icon.setImageDrawable(yuan);
            }else {
                DeliveryOrderAdapter.this.setNumber(DeliveryOrderAdapter.this.getNumber()+1);
                icon.setImageDrawable(xuanzhong);
            }

            item.setChoice(!item.isChoice());
        }
    }
}
